const formatTime = date => {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getDistance(lat1, lng1, lat2, lng2) {
  console.log("-----经纬度-----",lat1,lat2,lng1,lng2)
  var radLat1 = lat1 * Math.PI / 180.0;
  var radLat2 = lat2 * Math.PI / 180.0;
  var a = radLat1 - radLat2;
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
    Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137; // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  return s;

}
 function GetQueryString (url) {
  if (url.indexOf("?") != -1) {
    var index = url.indexOf('?') + 1;
    // 得到？后的字符串
    var str = url.substr(index); // postid=10457794&actiontip=保存修改成功')
    var paramsObj = {};
    // 字符串通过&标识，转为数组
   var arrs = str.split("&"); // ["postid=10457794", "actiontip=保存修改成功"]
    for (var i = 0; i < arrs.length; i++) {
      // 分别将 = 左右两边拆分为数组, 动态变为键值对
      paramsObj[arrs[i].split("=")[0]] = arrs[i].split("=")[1]
      //  arrs[i].split("=")   ["postid", "10457794"]
    }
  }
 return paramsObj
}
function getMonday(date){
  let day = new Date(date).getDay()||7;
  let mondayTime = new Date(date).getTime() - ((day-1)*24*60*60*1000);
  return formatTime(mondayTime);
}

const hour=date=>{
	const date1 = new Date(date)
	const day = date1.getDate();
	const hour = date1.getHours()
	const minute = date1.getMinutes()
  const second = date1.getSeconds()
  console.log([hour + day * 24, minute, second].map(formatNumber).join(':'))
	return [hour + day * 24, minute, second].map(formatNumber).join(':')
}
const getTime = function (startTime) {
  startTime=startTime.replace(/-/g, '/')
  var newDate = new Date().getTime();
	var reciprocal = (new Date(startTime).getTime()  - newDate)/1000;
	if (reciprocal<0){
		return '';
	}
	// 获取天、时、分、秒
  let day = parseInt(reciprocal / (60 * 60 * 24));
  if(day>0){
    return `${day}天`
  }
	let hour = parseInt(reciprocal % (60 * 60 * 24) / 3600);
	let minute = parseInt(reciprocal % (60 * 60 * 24) % 3600 / 60);
  let second = parseInt(reciprocal % (60 * 60 * 24) % 3600 % 60);
	return [hour+day*24, minute, second].map(formatNumber).join(':')
}
const formatTime1 = date => {
  if(typeof(date)!='object'){
    if (date.indexOf('T') > -1) {} else {
      date = date.replace(/-/g, '/')
    }
  }
  // 备选方案
  // date = String(date)
  // date = date.replace(/T/g, ' ')
  //  date = date.replace(/-/g, '/')
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour  =date.getHours();
  const minute  =date.getMinutes();
  return [year, month, day].map(formatNumber).join('-')+' '+[hour,minute].map(formatNumber).join(':')
}
module.exports = {
  formatTime: formatTime,
  formatTime1,
  getDistance: getDistance,
  GetQueryString:GetQueryString,
  formatNumber,
  getMonday,
  hour,
  getTime
}